package com.jzo2o.foundations.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServerMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServerService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ServerServiceImpl extends ServiceImpl<ServerMapper, Serve>  implements IServerService {
    @Resource
    private ServerMapper serverMapper;
    @Resource
    private ServeItemMapper serveItemMapper;
    @Resource
    private RegionMapper regionMapper;

    /**
     * 分页查询
     *
     * @param reqDTO 查询条件
     * @return 分页结果
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO reqDTO) {
        return PageHelperUtils.selectPage(reqDTO,
                () -> serverMapper.queryList(reqDTO.getRegionId()));
    }

    /**
     * 批量新增服务
     * @param serveUpsertReqDTOList 服务列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBatch(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        List<Serve> serves = BeanUtils.copyToList(serveUpsertReqDTOList, Serve.class);
        for (Serve serve : serves){
            ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
            Region region = regionMapper.selectById(serve.getRegionId());
            serve.setCityCode(region.getCityCode());
            serve.setPrice(serveItem.getReferencePrice());
            serve.setCreateTime(LocalDateTime.now());
            serve.setUpdateTime(LocalDateTime.now());
            serve.setCreateBy(UserContext.currentUser().getId());
            serve.setUpdateBy(UserContext.currentUserId());
        }
        this.saveBatch(serves);
    }

    /**
     * 上架服务
     * @param id
     */
    @Override
    public void onSale(Long id) {
        Serve serve = serverMapper.selectById(id);
        if (serve.getSaleStatus()!= 0 && serve.getSaleStatus() != 1){
            throw new CommonException("服务状态异常,上架失败");
        }
        ServeItem serveItem = serveItemMapper.selectOne(new LambdaQueryWrapper<ServeItem>()
                .eq(ServeItem::getId, serve.getServeItemId())
        );
        if (serveItem.getActiveStatus() != 2){
            throw new CommonException("服务项状态禁用,上架失败");
        }
        lambdaUpdate()
                .set(Serve::getSaleStatus,2)
                .eq(Serve::getId, id)
                .update();
    }

}
